#include "YesNoPortrol.h"


void YesNoPortrol::setYesFunc(CCObject* sender, SEL_YesFunc selector)
{
	_yesSender = sender;
	_yesSelector = selector;
}

bool YesNoPortrol::init(const char* titleStr, CCSize size)
{
	bool bRet = false;
	do {
		CC_BREAK_IF(!titleStr);
		CC_BREAK_IF(!ModalLayer::init(0));
		/* bg */
		_bg = CCScale9Sprite::create("dialog/sp9_bg.png");
		CC_BREAK_IF(!_bg);
		_bg->setContentSize(size);
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		_bg->setPosition(ccp(winSize.width / 2, winSize.height / 2));
		this->addChild(_bg);
		/* title */
		CCLabelTTF* title = CCLabelTTF::create(titleStr, "DroidSansFallbackFull", 30);
		CC_BREAK_IF(!title);
		title->setAnchorPoint(ccp(0.5, 1));
		title->setPosition(ccp(_bg->getPositionX(),
				       _bg->boundingBox().getMaxY() - 5));
		title->setColor(TITLE_TEXT_COLOR);
		this->addChild(title);
		/* two btn */
		_pMenu->setZOrder(1);
		CCMenuItemImage* yesBtn = CCMenuItemImage::create("dialog/yes_n.png","dialog/yes_s.png",
						  this, menu_selector(YesNoPortrol::yesCB));
		CC_BREAK_IF(!yesBtn);
		yesBtn->setAnchorPoint(CCPointZero);
		yesBtn->setPosition(ccp(_bg->boundingBox().getMinX() + 30,
					_bg->boundingBox().getMinY() + 15));
		_pMenu->addChild(yesBtn);

		CCMenuItemImage* noBtn = CCMenuItemImage::create("dialog/no_n.png","dialog/no_s.png",
						  this, menu_selector(YesNoPortrol::noCB));
		CC_BREAK_IF(!noBtn);
		noBtn->setAnchorPoint(ccp(1, 0));
		noBtn->setPosition(ccp(_bg->boundingBox().getMaxX() - 30,
		                       yesBtn->getPositionY()));
		_pMenu->addChild(noBtn);

		/* two line */
		_lineBatch = CCSpriteBatchNode::create("dialog/dline.png", 2);
		CC_BREAK_IF(!_lineBatch);
		_lineBatch->setPosition(CCPointZero);
		this->addChild(_lineBatch);
		CCSprite* line = CCSprite::createWithTexture(_lineBatch->getTexture());
		CC_BREAK_IF(!line);
		line->setAnchorPoint(title->getAnchorPoint());
		line->setPosition(ccp(_bg->getPositionX(), title->boundingBox().getMinY()));
		const float sc = (_bg->getContentSize().width - 20) / line->getContentSize().width;
		line->setScale(sc);
		_lineBatch->addChild(line, 0, 0);

		line = CCSprite::createWithTexture(_lineBatch->getTexture());
		CC_BREAK_IF(!line);
		line->setAnchorPoint(ccp(0.5, 0));
		line->setPosition(ccp(_bg->getPositionX(), yesBtn->boundingBox().getMaxY() + 5));
		line->setScale(sc);
		_lineBatch->addChild(line, 0, 1);

		bRet = true;
	}while(0);
	return bRet;
}

void YesNoPortrol::noCB(CCObject* obj)
{
	this->removeFromParentAndCleanup(true);
}

void YesNoPortrol::yesCB(CCObject* obj)
{
	if (_yesSender && _yesSelector) {
		(_yesSender->*_yesSelector)(this);
	}
	this->removeFromParentAndCleanup(true);
}
